package com.instasale.dao;

import com.instasale.models.Feature;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Repository
public class HibernateFeatureDAO implements FeatureDAO {

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    public List<Feature> listFeature() {
        return hibernateTemplate.find("from " + Feature.class.getName());
    }

    @Override
    public Set<Feature> getByIds(Integer[] ids) {
        Query query = hibernateTemplate.getSessionFactory()
                .openSession()
                .createQuery("from Feature f where f.id in (:ids)").setParameterList("ids", ids);
        List<Feature> features = query.list();
        return new HashSet<Feature>(features);
    }


}
